package com.cwj.lab3.博客需要;

import com.cwj.lab1.myArrayList;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
/**
 * 邻接矩阵存储
 * */
public class Graph_matrix_2 {
    //结点结构
    private static class Node{
        Integer data;
        String name;

        public Node(Integer data, String name) {
            this.data = data;
            this.name = name;
        }
    }
    //点表：结点对于的索引就是其编号，和下面map的值相对应
    private final myArrayList<Node>nodeList;
    //用来处理编号和名称得映射表
    private HashMap<String,Integer>map;
    //点数
    private int v_num;
    //边数
    private int e_num;
    //二维数组
    private int[][]graph;
    //边不存在的标志
    private static final int INF=Integer.MAX_VALUE;

    public Graph_matrix_2() {
        nodeList=new myArrayList<>();
    }
    //建树（仅仅建边的关系
    private void init(int v_num,int e_num){
        this.v_num=v_num;
        this.e_num=e_num;
        graph=new int[v_num][v_num];
        for (int i = 0; i < this.v_num; i++) {
            for (int j = 0; j < this.v_num; j++) {
                this.graph[i][j]=INF;
            }
        }
    }
    //建树（还有建点的信息）
    public void init_Full(String path) throws FileNotFoundException {
        map=new HashMap<>();
        Scanner in=new Scanner(new File(path));
        init(in.nextInt(),in.nextInt());
        Integer data=null;
        String name=null;
        for (int i = 0; i < this.v_num; i++) {
           name=in.next(); data=in.nextInt();
            nodeList.addLast(new Node(data,name));
            map.put(name,i);
        }
        String u=null,v=null;
        for (int i = 0; i < e_num; i++) {
            u=in.next();v=in.next();
            this.graph[map.get(u)][map.get(v)]=in.nextInt();
        }
    }
    //输出图
    public void showGraph(){
        for (int i = 0; i < this.v_num; i++) {
            for (int j = 0; j < this.v_num; j++) {
                int temp=this.graph[i][j];
                if(temp!=INF){
                    System.out.print(nodeList.get(i).name);
                    System.out.print("-->");
                    System.out.println(nodeList.get(j).name+"\tval="+temp);
                }
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        String path="src/src/com/cwj/lab3/博客需要/有名称的图信息";
        Graph_matrix_2 graph=new Graph_matrix_2();
        graph.init_Full(path);
        graph.showGraph();
    }
}
